import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import BMI from "../views/BMI";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/m",
    name: "M",
    component: () => import("@/views/M"),
    children: [
      {
        path: "",
        name: "MHome",
        component: () => import("@/views/m/Home"),
      },
      {
        path: "classify",
        name: "MClassify",
        component: () => import("@/views/m/Classify"),
      },
      {
        path: "cart",
        name: "MCart",
        component: () => import("@/views/m/Cart"),
      },
      {
        path: "mine",
        name: "MMine",
        component: () => import("@/views/m/Mine"),
      },
    ],
  },
  {
    path: "/admin",
    name: "Admin",
    component: () => import("@/views/Admin"),
    children: [
      {
        // /admin
        path: "",
        name: "AdminHome",
        component: () => import("@/views/admin/Home"),
      },
      {
        // /admin/posts
        path: "posts",
        name: "AdminPosts",
        component: () => import("@/views/admin/Posts"),
        meta: {
          "test-meta": "test-meta的值",
          title: "我是当前页面meta来的title",
        },
      },
      {
        // /admin/menu
        path: "menu",
        name: "AdminMenu",
        component: () => import("@/views/admin/Menu"),
      },
    ],
  },
  {
    path: "/article/:id",
    name: "Article",
    component: () => import("@/views/Article"),
  },
  {
    path: "/user/update",
    name: "UserUpdate",
    component: () => import("@/views/UserUpdate"),
  },
  {
    path: "/user/:id/:index",
    name: "User",
    component: () => import("@/views/User"),
  },
  {
    path: "/index",
    // redirect: "/", // 重定向
    redirect: {
      name: "About",
    },
  },
  {
    path: "/bmi",
    name: "BMI",
    component: BMI,
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import("../views/About.vue"),
  },
  {
    path: "*",
    name: "NotFound",
    component: () => import("@/views/NotFound"),
  },
];

const router = new VueRouter({
  mode: "history",
  // mode: "hash",
  base: process.env.BASE_URL,
  routes,
  // linkExactActiveClass: 'active'
});

// 路由的钩子函数
router.beforeEach((to, from, next) => {
  // 权限判断
  // 登录信息
  // console.log('to', to)
  // console.log('from', from)
  next();
});

export default router;
